<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">




<jsp:include page="../template/includes.jsp"></jsp:include>

<jsp:include page="../marca/editarMarca.jsp"></jsp:include>

<link rel="stylesheet" type="text/css" href="../../static/styles/home.css">
<body style="background: #6C7B8B;">
	<div class="template-cadastro" >
		<div style="width: 250px; margin-left: 10px; margin-top: 10px;"> 
			<div style="margin-bottom: 5px;">Marca*</div>
			<input  type="text" id="pesquisar-marca" style="height: 20px; width: 250px;">
			<button class="default-button" id="btn-pesquisar-marca" style="font-size: 12px; margin-left: 260px; margin-top: -28px; width: 80px;">Pesquisar</button>
		</div>
		
		<div style="margin-left: 90px; margin-top: 30px;" >
		<table id= "grid-marca"></table>
		</div>
		
	<div class="popup-buttons-container" style="text-align: center;margin-bottom: -13px;">
			<button class="default-button" id="btn-novo-marca" style="font-size: 12px; margin-right: 10px; width: 80px;">Novo</button>
			<button class="default-button" id="btn-editar-marca" style="font-size: 12px; margin-right: 10px; width: 80px;">Editar</button>
			<button class="default-button" id="btn-excluir-marca" style="font-size: 12px; margin-right: 10px; width: 80px;">Excluir</button>
			<button class="default-button" id="btn-sair-marca" style="font-size: 12px; width: 80px;">Sair</button>
		</div>		
	</div>
</body>
	<script type="text/javascript">
	
	$("#grid-marca").jqGrid({
		datatype: "local", 
		colNames:['Codigo','Marca'], 
		colModel:[
		{ name: 'idMarca', index: 'idMarca', width: 50 },
		{ name: 'descricao', index: 'descricao', width: 200 },
		],
		rowNum:100, 
		rowList:[100,200,300], 
		viewrecords: true, 
		multiselect: false,
		width: 600,
		height: 350,
		scroll: true
		});
	
	MarcaController.listarMarca({
		callback: function(result){
			for(var i=0;i<=result.length;i++){
				$("#grid-marca").addRowData(i+1,result[i]);
			}
		}
	});
	
	$("#btn-editar-marca").click(function() {
		var rowidMarca = $("#grid-marca").jqGrid('getGridParam','selrow');
		var rowData = $("#grid-marca").getRowData( rowidMarca );
		
		if (rowData.idMarca != null) 
		{
			MarcaController.getMarcaById( rowData.idMarca,{
					callback: function(result){
						
						$("#id-marca").val(result.idMarca);
						$("#desc-marca").val(result.descricao);
						$("#datacadastro-marca").val(result.datacadastro);
			 			$("#dataalteracao-marca").val(result.dataalteracao);
					}
				});
			
			$("#editar-marca").dialog("open");
		
			$("#btn-salvar-marca").css("display","none");
			$("#btn-alterar-marca").css("display","block");
		}
		
	});
	
	$("#btn-excluir-marca").click(function() {
		var rowidMarca = $("#grid-marca").jqGrid('getGridParam','selrow');
		var rowData = $("#grid-marca").getRowData( rowidMarca );
		
		if (rowData.idMarca != null) 
		{
			MarcaController.getMarcaById( rowData.idMarca,{
					callback: function(result){
						
						$("#id-marca").val(result.idMarca);
						$("#desc-marca").val(result.descricao);
						$("#datacadastro-marca").val(result.datacadastro);
			 			$("#dataalteracao-marca").val(result.dataalteracao);
					}
				});
			
			$("#editar-marca").dialog("open");
		
			$("#desc-marca").attr("disabled","disabled");
			
			$("#btn-salvar-marca").css("display","none");
			$("#btn-alterar-marca").css("display","none");
			$("#btn-deletar-marca").css("display","block");
		}
	});
		
	$("#btn-novo-marca").click(function() {
		$("#editar-marca").dialog("open");
		$("#btn-alterar-marca").css("display", "none");
		$("#btn-salvar-marca").css("display", "block");
		$("#btn-deletar-marca").css("display", "none");
	});
	
	$("#btn-sair-marca").click(function() {
		location.href = "http://localhost:8080/project-mega-visao/appweb/development/index.jsp";
	});
	

	function atualizaGrid () {
		
		$("#grid-marca").jqGrid('clearGridData');
		
		MarcaController.listarMarca({
			callback: function(result)
			{
				for(var i=0;i<=result.length;i++)
				{
					$("#grid-marca").addRowData(i+1,result[i]);
				}
			}
		});
	}
	
	$("#btn-salvar-marca").click(function () {
		atualizaGrid();
	});
	
	$("#btn-editar-marca").click(function () {
		atualizaGrid();			
	});
$("#btn-pesquisar-marca").click(function() {
		
		if( $("#pesquisar-marca").val() == "")
		{
			$("#grid-marca").jqGrid('clearGridData');
			 
			 MarcaController.listarMarca({
					callback: function(result)
					{
						for(var i=0;i<=result.length;i++)
						{
							$("#grid-marca").addRowData(i+1,result[i]);
						}
					}
			});
		}
		else
		{
			$("#grid-marca").jqGrid('clearGridData');
			 
			 MarcaController.listarMarca({
					callback: function(result)
					{
						for(var i=0;i<=result.length;i++)
						{
							if( $("#pesquisar-marca").val() == result[i].marca)
							{
								$("#grid-marca").addRowData(i+1,result[i]);
							}
						}
					}
			});
		}
	});
	
	</script>
	
	
